@isTest
private class CaseCommentsComponentControllerTest
{
	private static String IOGIncidentRecordTypeName = 'IOG Incident';
    private static String IOGChangeTaskRecordTypeName = 'IOG Change Task';
	private static String IOGIncidentRecordTypeId =RecordTypeHelper.GetRecordTypeID('Case',  IOGIncidentRecordTypeName);
	private static String IOGChangeTaskRecordTypeId = RecordTypeHelper.GetRecordTypeID('Case', IOGChangeTaskRecordTypeName);

 	static testMethod void testRelatedCaseComments()
    {
		//create IOG incident case
		Case testIncidentCase = new Case(Description='IOG Incident Case ', RecordTypeId=IOGIncidentRecordTypeId,Status='Open',Origin='CRF');
		insert testIncidentCase;
		//insert test comments for Incident case
		CaseComment incidentComment = New CaseComment();
		incidentComment.parentId	= testIncidentCase.Id;
		incidentComment.CommentBody = 'IOG Incident Case comment';
		insert incidentComment;

		//create related cases - IOG Change Task
		Case testTaskCase = new Case(Description='IOG Change Task', RecordTypeId=IOGChangeTaskRecordTypeId,Status='Open',Origin='CRF' ,parentid=testIncidentCase.id);
		insert testTaskCase;

		//insert test comments for Incident case
		CaseComment changeComment = New CaseComment();
		changeComment.parentId		= testTaskCase.Id;
		changeComment.CommentBody 	= 'IOG Change Task comment 1';
		insert changeComment;

		//insert test comments for Incident case
		changeComment = New CaseComment();
		changeComment.parentId		= testTaskCase.Id;
		changeComment.CommentBody 	= 'IOG Change Task comment 2';
		insert changeComment;

		CaseCommentsComponentController caseCommentsController = new CaseCommentsComponentController();
    	//search by incident case
    	caseCommentsController.caseId= testIncidentCase.Id;
    	caseCommentsController.parentCaseId = null;

    	//should return the combined comments for both the related cases..
    	System.assertEquals(caseCommentsController.comments.size(),3);

    	//delete one comment
    	PageReference page = new PageReference('/apex/casecommentsPage?CommentId_d='+changeComment.Id);
  		Test.setCurrentPage(page);
    	caseCommentsController.deleteComment();

    	//should return one less (deleted comment)..
    	System.assertEquals(caseCommentsController.comments.size(),2);

    	//mark a comment as public
    	page = new PageReference('/apex/casecommentsPage?CommentId_p=' + incidentComment.Id);
  		Test.setCurrentPage(page);
    	caseCommentsController.makePublicPrivate();

    	//should return one less (deleted comment)..
    	System.assertEquals(caseCommentsController.comments.size(),2);

    	//check only the comment for incident case is marked as public!
    	//order of resultset is by latest comment..
    	System.assertEquals(caseCommentsController.comments[0].PublicPrivateAction , 'Make Private');//is published =tru
    	System.assertEquals(caseCommentsController.comments[1].PublicPrivateAction , 'Make Public'); //is published =false
    }

	// run a test that has over a 100 comments (there was code that was failing because it was querying SOQL in a loop)
	static testMethod void testLotsOfRelatedCaseComments()
	{
		//create IOG incident case
		Case testIncidentCase = new Case(Description='IOG Incident Case ', RecordTypeId=IOGIncidentRecordTypeId, Status='Open', Origin='CRF');
		insert testIncidentCase;

		//insert test comments for Incident case
		Integer numberOfComments = 101;
		List<CaseComment> caseComments = new List<CaseComment>();
		for (Integer i=0; i<numberOfComments; i++)
		{
			CaseComment comment = New CaseComment();
			comment.parentId = testIncidentCase.Id;
			comment.CommentBody = 'IOG Task comment ' + i.format();
			caseComments.Add(comment);
		}
		insert caseComments;

		// Begin the test
		Test.startTest();
		CaseCommentsComponentController caseCommentsController = new CaseCommentsComponentController();
		// search by incident case
		caseCommentsController.caseId = testIncidentCase.Id;
		caseCommentsController.parentCaseId = null;
		Test.stopTest();

		//should return the combined comments for both the related cases..
		System.assertEquals(caseCommentsController.comments.size(), numberOfComments);
	}
}